package action;

import entity.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import entity.PageBean;
import service.tAreaService;
import service.tDeviceService;
import service.tUserAndtDeviceService;
import service.tUserService;

public class SearchtDeviceAction {
	
	private Integer radio;
	private String searchvalue;
	private String userId;
	
	private String deviceArea;
	private String userName;
	private String industry;
	private Integer radios;
	private String values;
	private int pageSize;
	private int page;  //第几页
	private int page1;
	private int page2;
	private int page3;
	private String result;
    private PageBean pageBean;
	
	private tDeviceService tdeviceService;
	private tUserService tuserService;
	private tAreaService tareaService;
	private tUserAndtDeviceService uadservice;
	
	@SuppressWarnings("unchecked")
	public String search() throws IOException{
		
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);  
	    @SuppressWarnings("unused")
	    HttpSession session = ServletActionContext.getRequest().getSession();
		//Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		List<tUserAndtDevice> li = new ArrayList<tUserAndtDevice>();
		System.out.println("------------------这里是SearchtDeviceAction-----------------------");
		
		tUser users = (tUser) session.getAttribute("user");
		String loginId = users.getUserId();
		Integer Grade = users.getGrade();
		String tareaId = users.gettAreaId();
		System.out.println(loginId);
		System.out.println(radio);
		System.out.println(searchvalue);
		System.out.println("userId:"+userId);
		List<tArea> tarea = new ArrayList<tArea>();
		List<tDevice> lists = new ArrayList<tDevice>();
		JSONArray jsonArray = new JSONArray();
		
		li = uadservice.searchByuserId(loginId);
		
		if (radio == 0){
			List<tDevice> list1 = new ArrayList<tDevice>();
			if (Grade == 1){
				this.pageBean = tdeviceService.searchBydeviceNo(searchvalue,15,page);
				List<tDevice> list = pageBean.getList();
				for (tDevice tdevice : list){
					String tArea = tdevice.getDeviceArea();
					tarea = tareaService.searchByName(tArea);
					System.out.println(tarea);
					if (tarea.equals("") || tarea.equals(null)){
						break;
					}
					String tAeraId = tarea.get(0).getId();
					
					if (tAeraId.equals(tareaId)){
						list1.add(tdevice);
					}
					
				}
				request.setAttribute("lists",list1);
			}
			if (Grade == 2){
				this.pageBean = tdeviceService.searchBydeviceNo(searchvalue,15,page);
				List<tDevice> list = pageBean.getList();
				
				for (tDevice tdevice : list){
					JSONObject t = new JSONObject();
					
					t.put("deviceNo", tdevice.getDeviceNo());
					t.put("deviceName", tdevice.getDeviceName());
					t.put("addTime", tdevice.getAddTime());
					t.put("deviceArea", tdevice.getDeviceArea());
					t.put("place", tdevice.getPlace());
					t.put("deviceDescribe", tdevice.getDeviceDescribe());
					t.put("userId", userId);
					System.out.println(t);
				}
				request.setAttribute("lists",list);
			}
			if (Grade == 0){
				
				this.pageBean = tdeviceService.searchBydeviceNo(searchvalue,15,page);
				List<tDevice> list = pageBean.getList();
				
				for (tUserAndtDevice uad : li){
					String deviceId = uad.getDeviceId();
					for (tDevice tdevice : list){
						String tdeviceId = tdevice.getDeviceId();
						if (deviceId.equals(tdeviceId)){
							list1.add(tdevice);
						}
					}
				}
				request.setAttribute("lists",list1);
			}

			
			
		}
		
		if (radio == 1){
			System.out.println(searchvalue);
			List<tUser> tuser = new ArrayList<tUser>();
			tuser = tuserService.searchuserName(searchvalue);
			
			System.out.println(tuser);
			
			if (tuser.size() == 0){
				return "success";
			}
			
			Integer grade = tuser.get(0).getGrade();
			
			System.out.println(grade);
			System.out.println(Grade);
			
			if (grade == 1){
				
				if (Grade.equals(grade)){
					result = "您不能查询与您同级别的客户！请重新输入";
					response.setContentType("text/xml;charset=utf-8"); //更改字符编码
					System.out.println(result);
					return "error";
				}
				
				//tuser = tuserService.searchuserName(userName);
				String tAreaId = tuser.get(0).gettAreaId();
				System.out.println(tAreaId);
				//li = uadservice.searchByuserId(usersId);
				tarea = tareaService.searchById(tAreaId);
				String tAreaName = tarea.get(0).getName();
				
				System.out.println(tAreaName);
				
				this.pageBean = tdeviceService.searchBytDeviceArea(tAreaName,15,page);
				List<tDevice> list = pageBean.getList();
				for (tDevice tdevice : list){
					JSONObject t = new JSONObject();
					
					t.put("mac", tdevice.getMac());
					t.put("deviceId", tdevice.getDeviceId());
					t.put("deviceNo", tdevice.getDeviceNo());
					t.put("deviceName", tdevice.getDeviceName());
					t.put("addTime", tdevice.getAddTime());
					t.put("deviceArea", tdevice.getDeviceArea());
					t.put("place", tdevice.getPlace());
					t.put("deviceDescribe", tdevice.getDeviceDescribe());
					t.put("userId", userId);
					
					System.out.println(t);
				}
				request.setAttribute("lists",list);
			}
			List<tDevice> lists1=new ArrayList();
			if (grade == 0){
				
				String tAreaId = tuser.get(0).gettAreaId();
				String areaId = users.gettAreaId();
				if(Grade != 2){
					if (tAreaId.equals("")){
						result = "该用户所属区域不明，您无法查询！请重新输入";
						response.setContentType("text/xml;charset=utf-8"); //更改字符编码
						System.out.println(result);
						return "error";
					}
					
				}
				
				
				System.out.println(tAreaId+"              "+areaId);
				
				if (! tAreaId.equals(areaId)){
					result = "您查找的用户不属于您的区域，您无法查询！请重新输入";
					response.setContentType("text/xml;charset=utf-8"); //更改字符编码
					System.out.println(result);
					return "error";
				}
				
				String usersId = tuser.get(0).getUserId();
				System.out.println(usersId);
				this.pageBean = uadservice.searchByuserId(usersId, 15, page);
				List<tUserAndtDevice> lis = pageBean.getList();
				 for (tUserAndtDevice uad : lis){
					 String deviceId = uad.getDeviceId();
					 System.out.println(deviceId);
					 List<tDevice> list = new ArrayList<tDevice>();
					 list = tdeviceService.searchById(deviceId);
					 
					 //List<tDevice> list = pageBean.getList();
					
					 String mac = list.get(0).getMac();
					 String deviceid = list.get(0).getDeviceId();
					 String deviceNo = list.get(0).getDeviceNo();
					 String deviceName = list.get(0).getDeviceName();
					 Timestamp addTime = list.get(0).getAddTime();
					 String deviceArea = list.get(0).getDeviceArea();
					 String place = list.get(0).getPlace();
					 String deviceDescribe = list.get(0).getDeviceDescribe();
					 
					 JSONObject t = new JSONObject();
						
					 t.put("mac", mac);
					 t.put("deviceId", deviceid);
					 t.put("deviceNo", deviceNo);
					 t.put("deviceName", deviceName);
					 t.put("addTime", addTime);
					 t.put("deviceArea", deviceArea);
					 t.put("place", place);
					 t.put("deviceDescribe", deviceDescribe);
					 t.put("userId", userId);
					 
					 System.out.println(t);
					 lists1.addAll(list);
					 
				 }
				 request.setAttribute("lists",lists1);
			}
			
			
			
		}
		
		if (radio == 2){
			this.pageBean = tdeviceService.searchBytDeviceArea(searchvalue,15,page);
			List<tDevice> list = pageBean.getList();
			for (tDevice tdevice : list){
				JSONObject t = new JSONObject();
				
				t.put("deviceNo", tdevice.getDeviceNo());
				t.put("deviceName", tdevice.getDeviceName());
				t.put("addTime", tdevice.getAddTime());
				t.put("deviceArea", tdevice.getDeviceArea());
				t.put("place", tdevice.getPlace());
				t.put("deviceDescribe", tdevice.getDeviceDescribe());
				t.put("userId", userId);
				System.out.println(t);
			}
			request.setAttribute("lists",list);
		}
		
		List<tUser> tuser = new ArrayList<tUser>();
		
		tuser = tuserService.searchByGrade();
		
		for (tUser user : tuser){
			JSONObject t = new JSONObject();
			
			t.put("userName", user.getUserName());
			
			jsonArray.add(t);
		}
		
		//List<tArea> tarea = new ArrayList<tArea>();
		
		tarea = tareaService.searchAll();
		 for (tArea tareas : tarea){
			JSONObject t = new JSONObject();
				
			t.put("name", tareas.getName());
				
			jsonArray.add(t);
		 }
		 
		request.setAttribute("tarea",tarea);
		
		request.setAttribute("tuser",tuser);
		
		
		
		//System.out.println(lists.get(0).getDeviceId()+"  -------   "+lists.get(0).getDeviceName());
		
		return "success";
	}
	
	@SuppressWarnings("unchecked")
	public String searchfor() throws IOException{
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);  
	    @SuppressWarnings("unused")
		//Map session = context.getSession(); 
	    HttpSession session = ServletActionContext.getRequest().getSession();
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		
		System.out.println("------------------这里是SearchtDeviceAction-----------------------");
		
		tUser users = (tUser) session.getAttribute("user");
		String loginId = users.getUserId();
		Integer Grade = users.getGrade();
		String tareaId = users.gettAreaId();
		System.out.println(radios);
		List<tDevice> lists = new ArrayList<tDevice>();
		List<tUserAndtDevice> li = new ArrayList<tUserAndtDevice>();
		List<tArea> tarea = new ArrayList<tArea>();
		//List<tDevice> list = new ArrayList<tDevice>();
		
		li = uadservice.searchByuserId(loginId);
		JSONArray jsonArray = new JSONArray();
		if (radios == 0){
			System.out.println(deviceArea);
			if (Grade == 2){
				this.pageBean = tdeviceService.searchBytDeviceArea(deviceArea,15,page);
				List<tDevice> list = pageBean.getList();
				
				
				request.setAttribute("lists",list);
			}
			
			if (Grade == 0){
				this.pageBean = tdeviceService.searchBytDeviceArea(deviceArea,15,page);
				List<tDevice> list = pageBean.getList();
				List<tDevice> list1 = new ArrayList<tDevice>();
				for (tUserAndtDevice uad : li){
					String deviceId = uad.getDeviceId();
					for (tDevice tdevice : list){
						String tdeviceId = tdevice.getDeviceId();
						if (deviceId.equals(tdeviceId)){
							list1.add(tdevice);
						}
					}
				}
				request.setAttribute("lists",list1);
			}
			
		}
		
		if (radios == 1){
			System.out.println(userName);
			
			if (userName == "" || userName == null || userName.equals("")){
				return "success";
			}
			List<tUser> tuser = new ArrayList<tUser>();
			tuser = tuserService.searchuserName(userName);
			String tAreaId = tuser.get(0).gettAreaId();
			System.out.println(tAreaId);
			//li = uadservice.searchByuserId(usersId);
			tarea = tareaService.searchById(tAreaId);
			String tAreaName = tarea.get(0).getName();
			
			System.out.println(tAreaName);
			//int page1 = (page == 0 ? 0:page);
			this.pageBean = tdeviceService.searchBytDeviceArea(tAreaName,15,page1);
			List<tDevice> list = pageBean.getList();
			for (tDevice tdevice : list){
				JSONObject t = new JSONObject();
				
				t.put("radios", radios);
				t.put("mac", tdevice.getMac());
				t.put("deviceId", tdevice.getDeviceId());
				t.put("deviceNo", tdevice.getDeviceNo());
				t.put("deviceName", tdevice.getDeviceName());
				t.put("addTime", tdevice.getAddTime());
				t.put("deviceArea", tdevice.getDeviceArea());
				t.put("place", tdevice.getPlace());
				t.put("deviceDescribe", tdevice.getDeviceDescribe());
				t.put("userId", userId);
				
				System.out.println(t);
			}
			 request.setAttribute("lists",list);
		}
		
		if (radios == 2){
			System.out.println(industry);
			//int page2 = (page == 0 ? 0:page);
			if (Grade == 1){
				this.pageBean = tdeviceService.searchByIndustry(industry,15,page2);
				List<tDevice> list = pageBean.getList();
				List<tDevice> list1 = new ArrayList<tDevice>();
				for (tDevice tdevice : list){
					String tArea = tdevice.getDeviceArea();
					tarea = tareaService.searchByName(tArea);
					System.out.println(tarea);
					if (tarea.equals("") || tarea.equals(null)){
						break;
					}
					String tAeraId = tarea.get(0).getId();
					
					if (tAeraId.equals(tareaId)){
						list1.add(tdevice);
					}
					
				}
				request.setAttribute("lists",list1);
			}
			if (Grade == 0){
				this.pageBean = tdeviceService.searchByIndustry(industry,15,page2);
				List<tDevice> list = pageBean.getList();
				List<tDevice> list1 = new ArrayList<tDevice>();
				for (tUserAndtDevice uad : li){
					String deviceId = uad.getDeviceId();
					for (tDevice tdevice : list){
						String tdeviceId = tdevice.getDeviceId();
						if (deviceId.equals(tdeviceId)){
							list1.add(tdevice);
						}
					}
				}
				request.setAttribute("lists",list1);
			}
			
			if (Grade == 2){
				this.pageBean = tdeviceService.searchByIndustry(industry,15,page2);
				List<tDevice> list = pageBean.getList();
				
				for (tDevice tdevice : list){
					JSONObject t = new JSONObject();
					
					t.put("deviceNo", tdevice.getDeviceNo());
					t.put("deviceName", tdevice.getDeviceName());
					t.put("addTime", tdevice.getAddTime());
					t.put("deviceArea", tdevice.getDeviceArea());
					t.put("place", tdevice.getPlace());
					t.put("deviceDescribe", tdevice.getDeviceDescribe());
					t.put("userId", userId);
					System.out.println(t);
				}
				request.setAttribute("lists",list);
			}
			
		}
		
		if (radios == 3){
			System.out.println(values);
			//int page3 = (page == 0 ? 0:page);
			if (Grade == 1){
				this.pageBean = tdeviceService.seatchdeviceNamevalue(values,15,page3);
				List<tDevice> list = pageBean.getList();
				List<tDevice> list1 = new ArrayList<tDevice>();
				for (tDevice tdevice : list){
					String tArea = tdevice.getDeviceArea();
					tarea = tareaService.searchByName(tArea);
					System.out.println(tarea);
					if (tarea.equals("") || tarea.equals(null)){
						break;
					}
					String tAeraId = tarea.get(0).getId();
					
					if (tAeraId.equals(tareaId)){
						list1.add(tdevice);
					}
					
				}
				request.setAttribute("lists",list1);
			}
			if (Grade == 0){
				this.pageBean = tdeviceService.seatchdeviceNamevalue(values,15,page3);
				List<tDevice> list = pageBean.getList();
				List<tDevice> list1 = new ArrayList<tDevice>();
				for (tUserAndtDevice uad : li){
					String deviceId = uad.getDeviceId();
					for (tDevice tdevice : list){
						String tdeviceId = tdevice.getDeviceId();
						if (deviceId.equals(tdeviceId)){
							list1.add(tdevice);
						}
					}
				}
				request.setAttribute("lists",list1);
			}
			
			if (Grade == 2){
				this.pageBean = tdeviceService.seatchdeviceNamevalue(values,15,page3);
				List<tDevice> list = pageBean.getList();
				
				for (tDevice tdevice : list){
					JSONObject t = new JSONObject();
					
					t.put("deviceNo", tdevice.getDeviceNo());
					t.put("deviceName", tdevice.getDeviceName());
					t.put("addTime", tdevice.getAddTime());
					t.put("deviceArea", tdevice.getDeviceArea());
					t.put("place", tdevice.getPlace());
					t.put("deviceDescribe", tdevice.getDeviceDescribe());
					t.put("userId", userId);
					System.out.println(t);
				}
				request.setAttribute("lists",list);
			}
			
			
		}
		
		
		List<tUser> tuser = new ArrayList<tUser>();
		
		tuser = tuserService.searchByGrade();
		
		for (tUser user : tuser){
			JSONObject t = new JSONObject();
			
			t.put("userName", user.getUserName());
			
			jsonArray.add(t);
		}
		
		//List<tArea> tarea = new ArrayList<tArea>();
		
		tarea = tareaService.searchAll();
		 for (tArea tareas : tarea){
			JSONObject t = new JSONObject();
				
			t.put("name", tareas.getName());
				
			jsonArray.add(t);
		 }
		 
		request.setAttribute("tarea",tarea);
		
		request.setAttribute("tuser",tuser);
		
		//request.setAttribute("lists",lists);
		//request.setAttribute("li",li);
		//request.setAttribute("list",list);
		
		return "success";
	}

	public tDeviceService getTdeviceService() {
		return tdeviceService;
	}

	public void setTdeviceService(tDeviceService tdeviceService) {
		this.tdeviceService = tdeviceService;
	}
	

	public Integer getRadio() {
		return radio;
	}

	public void setRadio(Integer radio) {
		this.radio = radio;
	}

	public String getSearchvalue() {
		return searchvalue;
	}

	public void setSearchvalue(String searchvalue) {
		this.searchvalue = searchvalue;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public tUserService getTuserService() {
		return tuserService;
	}

	public void setTuserService(tUserService tuserService) {
		this.tuserService = tuserService;
	}

	public tUserAndtDeviceService getUadservice() {
		return uadservice;
	}

	public void setUadservice(tUserAndtDeviceService uadservice) {
		this.uadservice = uadservice;
	}

	public String getDeviceArea() {
		return deviceArea;
	}

	public void setDeviceArea(String deviceArea) {
		this.deviceArea = deviceArea;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getIndustry() {
		return industry;
	}

	public void setIndustry(String industry) {
		this.industry = industry;
	}

	public Integer getRadios() {
		return radios;
	}

	public void setRadios(Integer radios) {
		this.radios = radios;
	}

	public String getValues() {
		return values;
	}

	public void setValues(String values) {
		this.values = values;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public tAreaService getTareaService() {
		return tareaService;
	}

	public void setTareaService(tAreaService tareaService) {
		this.tareaService = tareaService;
	}

	public int getPage1() {
		return page1;
	}

	public void setPage1(int page1) {
		this.page1 = page1;
	}

	public int getPage2() {
		return page2;
	}

	public void setPage2(int page2) {
		this.page2 = page2;
	}

	public int getPage3() {
		return page3;
	}

	public void setPage3(int page3) {
		this.page3 = page3;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	

}
